// pages/home/home.js
//获取应用实例
const app = getApp()
var common = require('../../utils/common.js');
var that = "";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    isLogon: app.globalData.isLogin,
    page: 1,
    totalfee: 0,
    chargefee: 0,
    remainfee: 0,
    detailFeeInfo: [],
    initDataB: true,
    total:0//订单总数
  },
  /**
   * 点击每一项费用
   */
  chargesItemClick: function (e) {
    var chargeid = e.currentTarget.dataset.chargeid
    wx.navigateTo({
      url: '../receiptDetails/receiptDetails?chargeid=' + chargeid,
    })
  },
  toLogin: function () {
    app.toLogin();
  },
  //点击应收总额
  receivable: function () {
    wx.navigateTo({
      url: '../receivable/receivable',
    })
  },
  //点击已收租金总额
  received: function () {
    wx.navigateTo({
      url: '../received/received',
    })
  },
  //点击未收租金总额
  uncollected: function () {
    wx.navigateTo({
      url: '../uncollected/uncollected',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    // 自动登录
    common.autoLogin(this.getLogin);
    options.chargeid
    this.setData({
      page: 1,
    })
  },
  //点击查账
  toBill: function () {
    wx.navigateTo({
      url: '../bill/bill',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (this.data.isLogon) {
      // wx.showLoading({
      //   title: '加载中',
      // })
      // this.getDataListHttps(true);
      // this.getDataHeadHttps();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogon: app.globalData.isLogin,
    })
    if (this.data.isLogon && this.data.initDataB) {
      // this.getDataListHttps(true);
      this.getDataHeadHttps();
      this.getOrderSum();
    }
    //添加合同刷新
    var addContract = wx.getStorageSync('addContract');
    wx.removeStorageSync('addContract');
    var refresh = wx.getStorageSync('refresh');
    wx.removeStorageSync('refresh');
    var homeRefresh = wx.getStorageSync('homeRefresh');
    wx.removeStorageSync('homeRefresh');
    if (refresh || addContract || app.getRefresh() || homeRefresh) {
      this.setData({
        page: 1,
      })
      this.getDataHeadHttps();
      this.getOrderSum();
      app.removeRefresh();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //下拉刷新
    if (this.data.isLogon) {
      this.setData({
        page: 1,
      })
      // this.getDataListHttps(true);
      this.getDataHeadHttps();
      this.getOrderSum();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉获取跟多数据
    // if (this.data.isLogon) {
    //   this.getDataListHttps(false)

    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * https请求头部获取数据
   */
  getDataHeadHttps: function () {
    var that = this;
    //获取数据
    common._get('/wx/landlord/index', {},
      //成功
      function (res) {
        console.log('ssssssss');
        console.log(res.data);
        if (res.data.success) {
          that.setData({
            totalfee: res.data.data.totalfee,
            chargefee: res.data.data.chargefee,
            remainfee: res.data.data.remainfee
          })
          wx.stopPullDownRefresh();
        }
      });


  },
  /**
   * https请求列表数据
   * isRefresh true 刷新数据, false加载更多
   */
  // getDataListHttps: function (isRefresh) {
  //   var that = this;
  //   common._get('/wx/landlord/feelist', { page: that.data.page },
  //     //成功
  //     function (res) {
  //       if (res.data.success) {
  //         that.data.initDataB = false;
  //         var temp = []
  //         if (isRefresh) {
  //           temp = res.data.data.list
  //           wx.stopPullDownRefresh()
  //         } else {
  //           temp = that.data.detailFeeInfo.concat(res.data.data.list)
  //         }
  //         //获取数据成功
  //         that.setData({
  //           detailFeeInfo: temp,
  //           page: res.data.data.currPage + 1
  //         })
  //       }
  //     });

  // },

  //登录
  getLogin: function (params) {
    var that = this;
    common._post('/wx/landlord/login', params,
      //成功
      function (res) {
        console.log(res)
        if (res.data.success) {
          wx.showToast({
            title: res.data.msg,
          })
          wx.setStorageSync('result', 'result');
          wx.setStorageSync('token', res.data.token);
          that.setData({
            isLogon: true
          })
          app.globalData.isLogin = true;
          //更新首页数据
          if (that.data.initDataB) {
            that.getDataHeadHttps();
            that.getOrderSum();
          }

        } else {
          if (res.data.token && res.data.token != '') {
            wx.showToast({
              title: res.data.msg,
              image: '../../image/shibai.png'
            })
            wx.setStorageSync('utoken', res.data.token);
            app.toLogin();
          }
          //判断是否未审核
          if (res.data.code && res.data.code == '503') {
            app.globalData.isAudit = true;
          }
        }
      });
  },
  //获取账单总数
  getOrderSum:function(){
    common._get('/wx/landlord/charge/total', {},
      //成功
      function (res) {
        if (res.data.success) {
         that.setData({
           total: res.data.total
         })
        }
      });
  },
  //租约
  toLease:function(){
      wx.navigateTo({
        url: '../wdht/wdht',
      })
  },
  //未租和已租
  toHouseLise: function () {
    wx.navigateTo({
      url: '../unoccupiedAndHaveRent/unoccupiedAndHaveRent?showtab=0',
    })
  }

})